﻿Imports System.Data.OleDb

Public Class EditTags
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub grvEditTags_SelectedIndexChanged(sender As Object, e As EventArgs) Handles grvEditTags.SelectedIndexChanged
        Try

        
        Dim getTag As TagsManagement = New TagsManagement()
        Dim dr As OleDbDataReader = getTag.SelectTagByTagId(Convert.ToInt32(grvEditTags.SelectedValue))
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("TagName")) Then txtContent.Text = dr("TagName").ToString()
            If Not IsDBNull(dr("Visible")) Then ChBVisible.Checked = Convert.ToBoolean(dr("Visible"))
        End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Try

            If (grvEditTags.SelectedValue <> Nothing) Then

                'check if tag content has been changed 
                Dim tagOriginalContent As String = ""

                Dim getTag As TagsManagement = New TagsManagement()
                Dim dr As OleDbDataReader = getTag.SelectTagByTagId(Convert.ToInt32(grvEditTags.SelectedValue))
                If dr.HasRows Then
                    dr.Read()
                    If Not IsDBNull(dr("TagName")) Then tagOriginalContent = dr("TagName").ToString()
                End If

                Dim getTagCount As Int32 = 0
                If (tagOriginalContent <> txtContent.Text) Then
                    'check if tag already exists
                    Dim countTag As TagsManagement = New TagsManagement()
                    getTagCount = countTag.SelectCountTagsByTagName(txtContent.Text, Convert.ToInt32(grvEditTags.SelectedValue))

                End If

                If (getTagCount > 0) Then
                    lblMessage.Text = "Tag already exists. Update not done"
                Else
                    Dim editTag As TagsManagement = New TagsManagement()
                    editTag.EditTagByTagId(Convert.ToInt32(grvEditTags.SelectedValue), txtContent.Text, ChBVisible.Checked)

                    grvEditTags.DataBind()

                    lblMessage.Text = "Update has been done"

                End If

            Else
                lblMessage.Text = "Select value first"
            End If

        Catch ex As Exception

        End Try
    End Sub
End Class